package org.fast.easy.system.upms.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.fast.easy.core.domain.auth.model.LoginUser;
import org.fast.easy.core.util.MapstructUtils;
import org.fast.easy.framework.satoken.utils.LoginHelper;
import org.fast.easy.system.upms.domain.entity.SysUserShortcutMenu;
import org.fast.easy.system.upms.domain.vo.SysMenuVo;
import org.fast.easy.system.upms.domain.vo.SysUserShortcutMenuVo;
import org.fast.easy.system.upms.mapper.SysUserShortcutMenuMapper;
import org.fast.easy.system.upms.service.ISysUserShortcutMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类说明
 *
 * @author YanMing
 * @date 2024/6/18
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysUserShortcutMenuServiceImpl implements ISysUserShortcutMenuService {

    private final SysUserShortcutMenuMapper baseMapper;


    @Override
    public List<SysUserShortcutMenuVo> initShortcutMenuList( List<SysMenuVo> menuList) {
        List<SysUserShortcutMenuVo> menuShortcutList = new ArrayList<SysUserShortcutMenuVo>();
        if(menuList!=null && menuList.size()>0){
            LoginUser loginUser = LoginHelper.getLoginUser();
            if(loginUser!=null) {
                Long userId = loginUser.getUserId();
                String tenantId = loginUser.getTenantId();
                for (SysMenuVo menu : menuList) {
                    if (StringUtils.isNotBlank(menu.getPath())) {
                        if ("C".equals(menu.getMenuType())) {
                            if (menu.getMenuName() != null && menu.getMenuName().indexOf("管理") > 0) {
                                SysUserShortcutMenuVo menuShortcut = new SysUserShortcutMenuVo();
                                menuShortcut.setUserId(userId);
                                menuShortcut.setTenantId(tenantId);
                                menuShortcut.setMenuId(menu.getMenuId());
                                menuShortcut.setMenuName(menu.getMenuName());
                                menuShortcut.setMenuPath(menu.getPath());
                                menuShortcut.setMenuIcon(menu.getIcon());
                                menuShortcut.setUsedCount(1L);
                                menuShortcut.setUsedType(1L);
                                menuShortcut.setCreateTime(new Date());
                                menuShortcut.setUpdateTime(new Date());
                                menuShortcutList.add(menuShortcut);
                            }
                        }
                    }
                }
            }
        }
        addShortcutMenuVoList(menuShortcutList);
        return menuShortcutList;
    }

    @Override
    public Boolean addShortcutMenuVoList(List<SysUserShortcutMenuVo> menuList) {
        if(menuList!=null && menuList.size()>0){
            List<SysUserShortcutMenu> list = MapstructUtils.convert(menuList, SysUserShortcutMenu.class);
            if(list!=null && list.size()>0) {
                return insertOrUpdateBatch(list);
            }
        }
        return false;
    }

    @Override
    public Boolean addShortcutMenuList(List<SysUserShortcutMenu> menuList) {
        if(menuList!=null && menuList.size()>0){
            return insertOrUpdateBatch(menuList);
        }
        return false;
    }

    @Override
    public List<SysUserShortcutMenuVo> selectShortcutMenuList() {
        List<SysUserShortcutMenuVo> list = null;
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser!=null) {
            Long userId = loginUser.getUserId();
            String tenantId = loginUser.getTenantId();
            list = baseMapper.selectShortcutMenuList(tenantId, userId);
        }
        return list;
    }

    @Override
    public Boolean updateShortcutMenuUsed(Long menuId) {
        if(menuId!=null){
            LoginUser loginUser = LoginHelper.getLoginUser();
            if(loginUser!=null) {
                Long userId = loginUser.getUserId();
                String tenantId = loginUser.getTenantId();
                SysUserShortcutMenu menu = baseMapper.selectOne(tenantId, userId, menuId);
                if (menu != null) {
                    if (menu.getUsedCount() != null) {
                        menu.setUsedCount(menu.getUsedCount() + 1);
                    } else {
                        menu.setUsedCount(1L);
                        menu.setUsedType(1L);
                        menu.setCreateTime(new Date());
                    }
                    menu.setTenantId(tenantId);
                    menu.setUpdateTime(new Date());
                    return baseMapper.update(menu) > 0 ? true : false;
                } else {
                    menu = new SysUserShortcutMenu();
                    menu.setUserId(userId);
                    menu.setTenantId(tenantId);
                    menu.setMenuId(menuId);
                    menu.setUsedCount(1L);
                    menu.setUsedType(1L);
                    menu.setCreateTime(new Date());
                    menu.setUpdateTime(new Date());
                    return baseMapper.insert(menu) > 0 ? true : false;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean setShortcutMenuFixed(Long menuId) {
        if(menuId!=null){
            LoginUser loginUser = LoginHelper.getLoginUser();
            if(loginUser!=null) {
                Long userId = loginUser.getUserId();
                String tenantId = loginUser.getTenantId();
                SysUserShortcutMenu menu = baseMapper.selectOne(tenantId, userId, menuId);
                if (menu != null) {
                    if (menu.getUsedCount() != null) {
                        menu.setUsedCount(menu.getUsedCount() + 1);
                    } else {
                        menu.setUsedCount(1L);
                        menu.setCreateTime(new Date());
                    }
                    menu.setTenantId(tenantId);
                    menu.setUsedType(0L);
                    menu.setUpdateTime(new Date());
                    return baseMapper.update(menu) > 0 ? true : false;
                } else {
                    menu = new SysUserShortcutMenu();
                    menu.setUserId(userId);
                    menu.setTenantId(tenantId);
                    menu.setMenuId(menuId);
                    menu.setUsedCount(1L);
                    menu.setUsedType(0L);
                    menu.setCreateTime(new Date());
                    menu.setUpdateTime(new Date());
                    return baseMapper.insert(menu) > 0 ? true : false;
                }
            }
        }
        return false;
    }


    public Boolean insertOrUpdateBatch(List<SysUserShortcutMenu> menuList){
        if(menuList!=null && menuList.size()>0){
           for(SysUserShortcutMenu menu:menuList){
               SysUserShortcutMenu shortcutMenu = baseMapper.selectOne(menu.getTenantId(), menu.getUserId(), menu.getMenuId());
               if(shortcutMenu!=null){
                   baseMapper.update(menu);
               }else{
                   baseMapper.insert(menu);
               }
           }
        }
        return false;
    }
}

